const { COPYFILE_EXCL } = require('constants');
let Koa = require('koa');

let app = new Koa();

let bodyparser = require('koa-bodyparser');

let Router = require('koa-router');

let query = require('./db/db');


let router = new Router();


app.use(bodyparser());


app.use(router.routes());

app.use(router.allowedMethods());

//接口
//一级分类
router.get('/api/oneClassify',async (ctx) => {
    let list = await query('select * from oneClassify');
    ctx.body = {
        code:1,
        data:list
    }
})

//、二级分类
router.get('/api/twoClassify',async (ctx) => {
    let {oneType} = ctx.request.query;
    if(oneType){
        let target = await query('select * from twoClassify where oneType=?',[oneType]);
        ctx.body = {
            code:1,
            data:target
        }
    }else{
        ctx.body = {
            code:2,
            msg:'参数有误'
        }
    }
})

//查询分类的列表  分页

router.get('/api/search',async (ctx) => {
    let {pagenum=1,limit=2,twoType} = ctx.request.query;

    let startIndex = (pagenum - 1) * limit; //起始下标

    //既有条件又有分页  

    let result = await query(`select * from list where twoType=? limit ${startIndex},${limit}`,[twoType]);

    ctx.body = {
        code:1,
        data:result
    }
})



app.listen(3000,() => {
    console.log("服务器启动成功")
})